package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCzMoney;
import com.vf.admin.miaodong.service.AfterCzMoneyService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;

/**
 * 钻石充值金额管理
 * @author anxiang
 */
@Controller
@RequestMapping("/after/cz")
public class AfterCzMoneyController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AfterCzMoneyService afterCzMoneyService;

    /**
     * 钻石充值金额列表
     * @param splitPage 分页
     * @param name 充值名
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPage,String name){
        try{
            Map<String,Object> map = new HashMap<>();
            map.put("name",name);
            List<AppCzMoney> list = afterCzMoneyService.findList(map, splitPage);
            request.setAttribute("list",list);
            request.setAttribute("name",name);
            request.setAttribute("splitPage",splitPage);
            return this.sucess("/web/after/cz/list");
        }catch (Exception e){
            logger.error("钻石充值金额异常",e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 添加钻石充值金额跳转
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        try{
            return this.sucess("/web/after/cz/add");
        }catch (Exception e){
            logger.error("获取钻石充值金额详情异常",e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 编辑钻石充值金额跳转
     * @param id id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id){
        try{
            AppCzMoney money = afterCzMoneyService.get(id);
            request.setAttribute("money",money);
            return this.sucess("/web/after/cz/edit");
        }catch (Exception e){
            logger.error("编辑钻石充值金额跳转异常",e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 新增钻石充值金额
     * @param money 实体类
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result saveliwu(AppCzMoney money){
        try{
            if(StringEx.isNull(money.getName())){
                return this.errorJSON("充值名不能为空");
            }
            if(StringEx.isNull(money.getMoney())){
                return this.errorJSON("充值金额不能为空");
            }
            if(money.getId() != null){
                AppCzMoney  czMoney = afterCzMoneyService.get(money.getId());
                czMoney.setName(money.getName());
                czMoney.setMoney(money.getMoney());
                czMoney.setState(money.getState() == null ? Constants.shiFou_STATE_FOU : money.getState());
                czMoney.setGive(money.getGive());
                afterCzMoneyService.saveOrUpdate(czMoney);
            }else{
                money.setState(money.getState() == null ? Constants.shiFou_STATE_FOU : money.getState());
                afterCzMoneyService.saveOrUpdate(money);
            }
            return this.sucessJSON("保存成功");
        }catch (Exception e){
            logger.error("新增钻石充值金额异常",e);
            return this.errorJSON("系统繁忙");
        }
    }






}
